<%--
  DataDic: 高旭
  Date: 2020/12/16 0016
--%>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>数据字典管理</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/jquery-easyui-1.8.6/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/jquery-easyui-1.8.6/themes/icon.css">
    <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.8.6/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.8.6/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.8.6/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>

<%--数据字典展示数据网格--%>
<table class="easyui-datagrid" id="dg" data-options="title:'数据字典管理',fit:true,pagination:true,rownumbers:true,fitColumns:true,toolbar:'#dg-tb',url:'${pageContext.request.contextPath}/dataDic/list.do'">
    <thead>
    <tr>
        <th data-options="field:'cb',checkbox:true,align:'center'"></th>
        <th data-options="field:'id',align:'center'" style="width: 50px">编号</th>
        <th data-options="field:'dataDicName',align:'center'" style="width: 100px">数据字典名</th>
        <th data-options="field:'dataDicValue',align:'center'" style="width: 100px">数据字典值</th>
    </tr>
    </thead>
</table>
<div id="dg-tb">
    <div>
        <a href="javascript:openDataDicAddDialog()" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加</a>
        <a href="javascript:openDataDicModifyDialog()" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改</a>
        <a href="javascript:deleteDataDic()" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">删除</a>
    </div>
    <div>
        <input class="easyui-combobox" id="s_dataDicName" data-options="label:'&nbsp;字典名：&nbsp;',panelHeight:'auto',editable:false,valueField:'dataDicName',textField:'dataDicName',url:'${pageContext.request.contextPath}/dataDic/dataDicNameComboList.do'" style="width: 200px">
        <input class="easyui-textbox" id="s_dataDicValue" data-options="label:'&nbsp;字典值：&nbsp;'" style="width: 200px">
        <a class="easyui-linkbutton" href="javascript:searchDataDic()" data-options="iconCls:'icon-search',plain:true">搜索</a>
    </div>
</div>

<%--数据字典添加/修改对话框--%>
<div class="easyui-dialog" id="dlg" data-options="closed:true,buttons:'#dlg-btns',onClose:closeDataDicDialog" style="width: 680px;height: 300px;padding: 10px 20px">
    <form id="fm" method="post">
        <table cellspacing="2px">
            <tr>
                <td><input class="easyui-combobox" id="dataDicName" name="dataDicName" data-options="required:true,label:'&nbsp;字典名：&nbsp;',panelHeight:'auto',valueField:'dataDicName',textField:'dataDicName',url:'${pageContext.request.contextPath}/dataDic/dataDicNameComboList.do',width:220">&nbsp;<span style="color: red">*</span></td>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td><input class="easyui-textbox" id="dataDicValue" name="dataDicValue" data-options="required:true,label:'&nbsp;字典值：&nbsp;',width:220">&nbsp;<span style="color: red">*</span></td>
            </tr>
        </table>
    </form>
</div>
<div id="dlg-btns">
    <a href="javascript:saveDataDic()" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">保存</a>
    <a href="javascript:closeDataDicDialog()" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">关闭</a>
</div>

</body>
</html>

<script>

    $(function () {
        $('#s_dataDicValue').each(function (index, element) {
            $(this).textbox('textbox').bind('keydown', function (e) {
                if (e.keyCode == 13) {   // 当按下回车键时接受输入的值。
                    searchCustomer();
                }
            });
        });
    });

    var url = "";

    function searchDataDic() {
        $("#dg").datagrid('load', {
            "dataDicName": $("#s_dataDicName").combobox("getValue"),
            "dataDicValue": $("#s_dataDicValue").textbox("getValue"),
        });
    }


    function openDataDicAddDialog() {
        $("#dlg").dialog("open").dialog("setTitle", "添加数据字典");
        url = "${pageContext.request.contextPath}/dataDic/save.do";
    }

    function openDataDicModifyDialog() {
        var selectedRows = $("#dg").datagrid("getSelections");
        if (selectedRows.length != 1) {
            $.messager.alert("系统提示", "请选择一条要编辑的数据！");
            return;
        }
        var selectedRow = selectedRows[0];
        $("#dlg").dialog("open").dialog("setTitle", "编辑数据字典");
        $("#fm").form("load", selectedRow);
        url = "${pageContext.request.contextPath}/dataDic/save.do?id=" + selectedRow.id;
    }

    function deleteDataDic() {
        var selectedRows = $("#dg").datagrid("getSelections");
        if (selectedRows.length == 0) {
            $.messager.alert("系统提示", "请选择要删除的数据！");
            return;
        }
        var idsArr = [];
        for (var i = 0; i < selectedRows.length; i++) {
            idsArr.push(selectedRows[i].id);
        }
        var idsStr = idsArr.join(",");
        $.messager.confirm("系统提示", "您确定要删除这<span style='color: red'>" + selectedRows.length + "</span>条记录吗？", function (r) {
            if (r) {
                $.post("${pageContext.request.contextPath}/dataDic/delete.do", {ids: idsStr}, function (result) {
                    if (result.success) {
                        $.messager.alert("系统提示", "已成功删除数据！");
                        $("#dg").datagrid("reload");
                    } else {
                        $.messager.alert("系统提示", "删除数据失败，请联系系统管理员！");
                    }
                }, "json");
            }
        });
    }

    function saveDataDic() {
        $("#fm").form("submit", {
            url: url,
            onSubmit: function () {
                return $(this).form("validate");
            },
            success: function (result) {
                var result = eval("(" + result + ")");
                if (result.success) {
                    $.messager.alert("系统提示", "保存成功！");
                    $("#fm").form("reset");
                    $("#dlg").dialog("close");
                    $("#dg").datagrid("reload");
                } else {
                    $.messager.alert("系统提示", "保存失败！");
                }
            }
        });
    }

    function closeDataDicDialog() {
        var dlgOptions = $("#dlg").dialog("options");
        if (dlgOptions.closed != true) {
            $("#dlg").dialog("close");
        }
        $("#fm").form("reset");
        $("#dg").datagrid("reload");

        $(".easyui-combobox").each(function (index, element) {
            $(this).combobox("reload");
        });
    }

</script>
